package com.myacg.editor.common.unit.button;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import com.myacg.editor.common.utils.FontUtils;
import com.myacg.editor.common.utils.JFrameUtils;

/**
 * 可激活的按钮 UI未进行编辑后期调整
 * @author Administrator
 *
 */
public abstract class ActivButton extends JLabel{
	private String buttonName;
	private final String buttonId; //按钮对应的id
	private Icon buttonIcon;
	private boolean activated; //是否激活标志
	public ActivButton(String buttonName,String buttonId){
		this.buttonName = buttonName;
		this.buttonId = buttonId;
		this.activated = false;
		this.init();
	    this.setText(buttonName);
	}
	private void init(){
		//加入点击事件对应的事件改动
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//调整激活按钮
				activated = !activated;
				if(activated){ //激活状态
					activated();
				}else{ //非激活状态
					unActivated(); 
				}
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				super.mouseEntered(e);
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			}
			@Override
			public void mouseExited(MouseEvent e) {
				super.mouseExited(e);
				setCursor(Cursor.getDefaultCursor());
			}
		});
	}
	private Color actColor = new Color(200 , 200, 200 );
	private Color unActColor = new Color(240 , 240, 240 );
	private Color bdColor = new Color(150 , 150, 150 );
	private Color unBdColor = new Color(200 , 200, 200 );
	@Override
	protected void paintComponent(Graphics g) {
		   super.paintComponent(g);
			 RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 3,3);
			 Graphics2D g2=(Graphics2D) g;
			//填充背景
			if(activated){
				g2.setColor(actColor);
			}else{
				g2.setColor(unActColor);
			}
			g2.fill(roundedRectangle);
			//设置边框
			if(activated){
				g2.setColor(bdColor);
				g2.draw(roundedRectangle);
				g2.setColor(Color.darkGray);
			}else{
				g2.setColor(Color.gray);
			}
			//绘制字体部分
			FontUtils.drawCenterStringByPercentage(buttonName,1.3, this, g2);
	}
	/**
	 * 触发激活按钮的事件
	 */
	public void activated(){
		this.activated = true;
		this.activatedEvent();
		repaint();
	}
	
	/**
	 * 触发取消激活按钮的事件
	 */
	public void unActivated(){
		  this.activated = false;
		  this.unActivatedEvent();
		  repaint();
	}
	//后延的方法
	public abstract void activatedEvent();
	public abstract void unActivatedEvent();
	
	public String getButtonName() {
		return buttonName;
	}
	public void setButtonName(String buttonName) {
		this.buttonName = buttonName;
	}
	public String getButtonId() {
		return buttonId;
	}
	public Icon getButtonIcon() {
		return buttonIcon;
	}
	public void setButtonIcon(Icon buttonIcon) {
		this.buttonIcon = buttonIcon;
	}
	public boolean isActivated() {
		return activated;
	}
	//demo
	public static void main(String[] args) {
		JFrame  jframe = new JFrame("测试激活按钮");
		jframe.getContentPane().setLayout(new FlowLayout());
		jframe.getContentPane().add(new ActivButton("测试激活按钮1","123"){
			@Override
			public void activatedEvent() {
				System.out.println("激活");
			}
			@Override
			public void unActivatedEvent() {
				System.out.println("不激活");
			}
		});
		JFrameUtils.setDefaultWindow(jframe, 500, 500);
	}
	
	
}
